package com.dede.swipeitem

import android.graphics.Color
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.RecyclerView.ViewHolder
import android.support.v7.widget.helper.ItemTouchHelper
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import com.hhl.gridpagersnaphelper.GridPagerSnapHelper
import kotlinx.android.synthetic.main.activity_main.*




class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val list = (0..50).map { it.toString() }.toMutableList()

        rv_list.adapter = SimpleAdapter(list)
        val layoutManager = GridLayoutManager(this, 2, GridLayoutManager.HORIZONTAL, false)
        rv_list.layoutManager = layoutManager
//        rv_list.layoutManager = LinearLayoutManager(this)
        val snapHelper = GridPagerSnapHelper()
        snapHelper.setRow(2).setColumn(4)
//        val snapHelper = GroupSnapHelper(8)
        snapHelper.attachToRecyclerView(rv_list)
        ItemTouchHelper(object : ItemTouchHelper.Callback() {
            override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: ViewHolder?): Int {
                val dragFlags: Int
                val swipeFlags: Int
                if (recyclerView.layoutManager is GridLayoutManager) {
                    dragFlags = ItemTouchHelper.UP or ItemTouchHelper.DOWN or ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT
                    swipeFlags = ItemTouchHelper.START or ItemTouchHelper.END
//                    swipeFlags = 0
                } else {
                    dragFlags = ItemTouchHelper.UP or ItemTouchHelper.DOWN
                    swipeFlags = ItemTouchHelper.START or ItemTouchHelper.END
                }
                return makeMovementFlags(dragFlags, swipeFlags)
            }

            override fun onMove(recyclerView: RecyclerView, viewHolder: ViewHolder, target: ViewHolder): Boolean {
                val fromPosition = viewHolder.adapterPosition
                val toPosition = target.adapterPosition
                list.add(toPosition, list.removeAt(fromPosition))
                recyclerView.adapter.notifyItemMoved(fromPosition, toPosition)
                return false
            }

            override fun onSwiped(viewHolder: ViewHolder, direction: Int) {
//                val position = viewHolder.adapterPosition
//
//                adapter.notifyItemRemoved(position)
//                data.remove(position)
            }
        }).attachToRecyclerView(rv_list)
    }

    class SimpleAdapter(val data: List<String>) : RecyclerView.Adapter<ViewHolder>() {

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val itemView = holder.itemView
            itemView.setBackgroundColor(Color.GRAY)
            val textView = itemView.findViewById<TextView>(android.R.id.text1)
            textView.text = data[position]
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val width = parent.context.resources.displayMetrics.widthPixels / 4
            val view = LayoutInflater.from(parent.context).inflate(android.R.layout.simple_list_item_1, parent, false)
            val params = RecyclerView.LayoutParams(width, -2)
            view.layoutParams = params
            return object : ViewHolder(view) {}
        }

        override fun getItemCount(): Int = data.size
    }

}
